package com.example.common;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private JwtInterceptor jwtInterceptor;

    // 添加自定义拦截器，设置拦截规则
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //排除登录、注册接口，不需要jwt的token验证，其他所有/api开头的接口都要token验证
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns("/api/capture")
                .excludePathPatterns("/api/admin/login")
                .excludePathPatterns("/api/test/**")
                .excludePathPatterns("/api/admin/register")
                .excludePathPatterns("/api/files/**")
                .excludePathPatterns("api/type/upload");
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix("/api",clazz -> clazz.isAnnotationPresent(RestController.class));
    }
}
